package com.tiger.novelAvinfo.base;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tiger.novelAvinfo.AVInfo;
import com.tiger.novelAvinfo.R;

import net.youmi.android.AdListener;
import net.youmi.android.AdView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;

public class CommonUtil {
	public static String fileSuffix = ".txt";
	
	public static String zhNameKey="zhName";
	public static String enNameKey="enName";
	public static String picNumKey="picNum";
	public static String picDrawablesKey="picDraw";
	public static String commentKey="comment";
	
	public static String filePath = "";
	public static interface sendMessage{
		public static final int READ_TXT=0X01;
	}
	public static interface listnerKeys{
		public static final String read_key="MenuActivityResponse";
	}
	/**
	 * 置入有米广告
	 * @author:tiger.chenlin (2011-5-11) 
	 * @file:CommonUtil.java 
	 * @return:void
	 * @param act
	 * @param listener
	 */
	public static void setAdView(Activity act, AdListener listener) {
		// 初始化广告视图
		AdView adView = new AdView(act, Color.GRAY, Color.WHITE, 50);

		// 设置监听者(可以不设置，对获取广告没有影响)
		adView.setAdListener(listener);

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.FILL_PARENT,
				FrameLayout.LayoutParams.WRAP_CONTENT);

		// 设置广告出现的位置(悬浮于底部)
		params.bottomMargin = 0;
		params.gravity = Gravity.BOTTOM;
		adView.setBackgroundColor(R.color.clr_blue);
		// 将广告视图加入Activity中
		act.addContentView(adView, params);
	}
	/**
	 * 根据女优的名字得到其图片和介绍信息
	 * @author:tiger.chenlin (2011-5-11) 
	 * @file:CommonUtil.java 
	 * @return:Map<String,Object>
	 * @param enName
	 * @return
	 */
	public static Map<String,Object> getAVPerson(Context context,String enName,String zhName){
		Map<String,Object> person=new HashMap<String,Object>();
		Integer picNum=0;
		Drawable[] picDraws=null;
		String comment="";
		AssetManager am = context.getAssets();
		try{
			String fileDir="file:///android_asset/"+enName+"/";
			String picDir=enName+"/pic";
			Log.i("av", "filepath:"+picDir);
			
			File pics=new File(picDir);
			if(pics!=null)
			{
				Log.i("av", "pics is not null");
				String filenames[]=am.list(picDir);
				if(filenames==null)
					Log.i("av", "filenames is null");
				picNum=filenames.length;
				Log.i("av", "picNum:"+picNum);
				picDraws=new Drawable[picNum];
				for(int i=0;i<filenames.length;i++){
					Log.i("av", "file:"+filenames[i]);
					InputStream is=am.open(picDir+"/"+filenames[i]);
					picDraws[i]=Drawable.createFromStream(is, (i+1)+".jpg");
//					=Drawable.createFromPath(fileDir+"pic/"+filenames[i]);
				}
			}else{
				Log.i("av", "pics is null");
			}
			comment=getTextByCustomPath(am.open(enName+"/detail.txt"));
			Log.i("av", "comment:"+comment);
			person.put(zhNameKey, zhName);
			person.put(enNameKey, enName);
			person.put(picNumKey, picNum);
			person.put(picDrawablesKey, picDraws);
			person.put(commentKey, comment);
		}catch(Exception exp){
			exp.printStackTrace();
		}
		return person;
	}
	/**
	 * 得到文本文件的内容，filename包括全路径
	 * @author:tiger.chenlin (2011-5-11) 
	 * @file:CommonUtil.java 
	 * @return:String
	 * @param fileName
	 * @return
	 */
	public static String getTextByCustomPath(InputStream is){
		String text="";
		try{
		BufferedReader br;
		br = new BufferedReader(
				new InputStreamReader(is));
		   StringBuffer expected = new StringBuffer();
		   String s ;
		   while ((s = br.readLine()) != null) {
		    expected.append(s).append("\n");
		   }
		   text=expected.toString();
		}catch(Exception exp){
			exp.printStackTrace();
		}
		return text;
	}
	/**
	 * 得到asset根目录下文本文件的内容
	 * @author:tiger.chenlin (2011-5-11) 
	 * @file:CommonUtil.java 
	 * @return:String
	 * @param context
	 * @param txtNum
	 * @return
	 */
	public static String getTextByTxt(Context context, String fileName) {
		String text = "";
		AssetManager am = context.getAssets();
		InputStream is = null;
		try {
			is = am.open(fileName);
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is));
			StringBuilder sb = new StringBuilder("");
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			text=sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return text;
	}
	
	/**
	 * 得到AV目录
	 * @author:tiger.chenlin (2011-5-11) 
	 * @file:CommonUtil.java 
	 * @return:String
	 * @param context
	 * @return
	 */
	public static List<AVInfo> getAVmenu(Context context) {
		List<AVInfo> infos=new ArrayList<AVInfo>();
		AVInfo info=null;
		String fileName = "index" + fileSuffix;
		AssetManager am = context.getAssets();
		InputStream is = null;
		try {
			is = am.open(fileName);
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is));
			String line = null;
			line=reader.readLine();//去掉第一行
			while ((line = reader.readLine()) != null) {
				if(line!=null)
				{
					String[] arr=line.split(",");
					int length=arr.length;
					info=new AVInfo();
					info.setCN_NAME_FOR_SORT(arr[0]);
					info.setEnglish_Name(arr[1]);
					info.setChinese_Name(arr[2]);
					if(length>=4)
						info.setJapanese_Name(arr[3]);
					if(length>=5)
						info.setFlag(arr[4]);
					infos.add(info);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return infos;
	}
}
